#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright [Gtlions Lai].
# Create Date:
# Update Date:
"""summarization ahout this script.

detail ahout this script

   Class(): summarization about Class
   ...
   function(): summarization about function
   ...
"""
__authors__ = '"Gtlions Lai" <gtlions.l@qq.com>'

import sys
from PyQt4 import QtGui, QtCore


class Main(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('Radio Button')
        self.setGeometry(300, 300, 300, 300)
        grid = QtGui.QGridLayout()
        self.setLayout(grid)

        lb1 = QtGui.QLabel(u'输入')
        grid.addWidget(lb1, 0, 0)
        self.le1 = QtGui.QLineEdit(u'输入')
        grid.addWidget(self.le1, 0, 1)

        self.bt1 = QtGui.QPushButton('add')
        self.bt2 = QtGui.QPushButton('edit')
        self.bt3 = QtGui.QPushButton('delete')
        self.bt4 = QtGui.QPushButton('delete all')
        grid.addWidget(self.bt1, 1, 1)
        grid.addWidget(self.bt2, 2, 1)
        grid.addWidget(self.bt3, 3, 1)
        grid.addWidget(self.bt4, 4, 1)

        self.lw1 = QtGui.QListWidget()
        grid.addWidget(self.lw1, 0, 2, 5, 2)

        self.connect(self.bt1, QtCore.SIGNAL('clicked()'), self.add)
        self.connect(self.bt2, QtCore.SIGNAL('clicked()'), self.edit)
        self.connect(self.bt3, QtCore.SIGNAL('clicked()'), self.delete)
        self.connect(self.bt4, QtCore.SIGNAL('clicked()'), self.deleteall)

    def add(self):
        self.lw1.addItem(self.le1.text())
        self.le1.setText('')

    def edit(self):
        row = self.lw1.currentRow()
        newtext, ok = QtGui.QInputDialog.getText(self, 'new', 'new v')
        if ok and len(newtext) != 0:
            self.lw1.takeItem(self.lw1.currentRow())
            self.lw1.insertItem(row, QtGui.QListWidgetItem(newtext))

    def delete(self):
        self.lw1.takeItem(self.lw1.currentRow())

    def deleteall(self):
        self.lw1.clear()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())